# makefile for carbon engine
# do not touch anything if you want it
# to work.

# compilation target preferences
target		?= carbon
standard	:= c++20
compiler	:= g++

# directories
build_dir	?= build
src_dir		?= src
inc_dir		:= $(shell find $(src_dir) -type d)

# compilation dependencies
sources		:= $(shell find $(src_dir) -name *.cpp -or -name *.c -or -name *.s)
objects		:= $(sources:%=$(build_dir)/%.o)
depends		:= $(objects:.o=.d)

# compilation flags
inc_flags	:= $(addprefix -I,$(inc_dir))
cxx_flags	?= $(inc_flags) -fsanitize=address -std=$(standard)
ld_flags	:= -lglfw -lGL -lGLEW

# main target
$(build_dir)/$(target): $(objects) makefile
	@echo -e "Building final target  -  \e[1;36m$(target)\e[m"
	@echo -e "Invoked                -  \e[1;36m$(compiler) compiler\e[m"
	@echo -e "C++ standard           -  \e[1;36m$(standard)\e[m"
	@$(compiler) $(objects) -o $@ -O2 $(ld_flags) $(cxx_flags)
	@echo -e "Built target           -  \e[1;36m$(target)\e[m \e[1;32mSUCCESSFULLY\e[m"
	@echo -e "Executable can be found in \e[1;36m$(build_dir)/$(target)\e[m"

# c++ target
$(build_dir)/%.cpp.o: %.cpp
	@mkdir -p $(dir $@)
	@echo -e "Compiling              -  \e[1;36m$<\e[m"
	@$(compiler) $(cxx_flags) -c $< -o $@

.PHONY: clean

clean:
	@rm -rf $(build_dir)/*

run: $(build_dir)/$(target)
	@./$(build_dir)/$(target)

-include $(depends)